/**
 * 快速排序阮一峰版
 * @param {number[]} nums
 * @return {number[]}
 */

const quickSort = arr => {
  if (arr.length < 2) return arr

  const mid = ~~(arr.length / 2)
  const pivod = arr.splice(mid, 1),
    left = [],
    right = []

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < pivod) {
      left.push(arr[i])
    } else {
      right.push(arr[i])
    }
  }

  return [...quickSort(left), ...pivod, ...quickSort(right)]
}

const sortArray = nums => {
  return quickSort(nums)
}

console.log(sortArray([1, 5, 4, 2, 3]))